package daily.play2;

/*import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class MyThread1 implements Callable<Integer> {
    ArrayList<Integer> list = new ArrayList<>();
    public MyThread1(ArrayList<Integer> list){
        this.list = list;
    }
    public Integer call(){
        ArrayList<Integer> listtemp = new ArrayList<>();
        while(true){
            synchronized (MyThread.class){
                if(list.size()!=0){
                    Collections.shuffle(list);
                    int temp = list.remove(0);
                    listtemp.add(temp);
                    System.out.println(Thread.currentThread().getName()+"抽到了"+temp);
                }else{
                    break;
                }
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println(Thread.currentThread().getName()+":"+listtemp);
        return Collections.max(listtemp);
    }
}

public class demo2{
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list,11,22,33,44,55,66,77,78);
        MyThread1 thread1 = new MyThread1(list);
        MyThread1 thread2 = new MyThread1(list);
        FutureTask<Integer> ft1 = new FutureTask<>(thread1);
        FutureTask<Integer> ft2 = new FutureTask<>(thread2);
        Thread thread3 = new Thread(ft1);
        Thread thread4 = new Thread(ft2);
        thread3.start();
        thread4.start();
        int n1 = ft1.get();
        int n2 = ft2.get();
        Thread.sleep(200);
        System.out.println(Math.max(n1,n2));

    }
}*/


/*import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class demo2{
    public static void main(String[] args) throws IOException {
        //创建一个通讯工具
        //无参就是随机使用一个默认的本机端口发送信息
        //有参就是指定使用某个端口发送信息
        DatagramSocket ds = new DatagramSocket();
        //打包数据
        String str = "Hello World";
        byte[] arr = str.getBytes();
        InetAddress address = InetAddress.getByName("127.0.0.1");
        int port = 1111;
        DatagramPacket dp = new DatagramPacket(arr,arr.length,address,port);
        //发送数据
        ds.send(dp);
        //释放资源
        ds.close();
    }
}*/
//import java.io.IOException;
//import java.net.DatagramPacket;
//import java.net.DatagramSocket;
//import java.net.SocketException;
//
////聊天室的接收端
//public class demo2{
//    public static void main(String[] args) throws IOException {
//        DatagramSocket ds = new DatagramSocket(1111);
//        while(true){
//            byte[] arr = new byte[1024];
//            DatagramPacket dp = new DatagramPacket(arr,arr.length);
//            ds.receive(dp);
//            System.out.println("接受的数据是："+new String(arr,0,dp.getLength()));
//        }
//    }
//}

//class Person{
//    int a;
//    public Person(int a) {
//        this.a = a;
//    }
//}
//class Student extends Person{
//    int b;
//    public Student(int a,int b){
//        super(a);
//        this.a = a;
//        this.b = b;
//    }
//}
//public class demo2{
//    public static void main(String[] args){
//        int [] arr = {1,2,3,4,5};
//        int [] arr2 = new int[3];
//        int [] arr3 = new int[]{2,3,4,5,6};
//        for (int i = 0; i < arr.length; i++) {
//            System.out.println(arr[i]);
//        }
//    }
//}

//import java.util.Scanner;
//
//public  class demo2{
//    public static void main(String[] args){
//        Scanner sc = new Scanner(System.in);
//        int t = sc.nextInt();
//        if(t<=30&&t>=25){
//            System.out.println("ok!");
//        }else{
//            System.out.println("no!");
//        }
//    }
//}

import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//
//public class demo2{
//    public static void main(String[] args){
//        Scanner sc = new Scanner(System.in);
//        double weight = sc.nextDouble();
//        if(weight<=20){
//            System.out.printf("%.2f",weight*1.68);
//        }else{
//            System.out.printf("%.2f",weight*1.98);
//        }
//    }
//}

//public class demo2 {
//    public static void main(String[] args) {
//      /*  Scanner sc = new Scanner(System.in);
//        double number = sc.nextDouble();
//        System.out.printf("%.2f",Math.abs(number));*/
//        Scanner sc = new Scanner(System.in);
//        char temp = sc.nextLine().charAt(0);
//        if((int)temp%2==1){
//            System.out.println("YES");
//        }else{
//            System.out.println("NO");
//        }
//    }
//}

//多线程的学习
//第一种实现方式
/*class MyThread extends Thread{
        public void run(){
                for (int i = 0; i < 100; i++) {
                        System.out.println(getName()+(i+1));
                }
        }
}
public class demo2{*/
/*        public static void main(String[] args){
                MyThread t1=  new MyThread();
                t1.setName("线程1");
                t1.start();
        }
}*/

//第二种实现方式
/*class Mythread implements Runnable{
        public Mythread(){}

        public void run(){
                for (int i = 0; i < 101; i++) {
                        System.out.println(Thread.currentThread().getName()+(i+1));
                }
        }
}
public class demo2{
        public static void main(String[] args){
                Mythread t = new Mythread();
                Thread t1= new Thread(t);
                t1.setName("线程测试");
                t1.start();
        }
}*/

//第三种实现方式
/*class MyCallable implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
                int sum=0;
                for (int i = 0; i < 100; i++) {
                        sum+=i;
                }
                return sum;
        }
}
public class demo2{
        public static void main(String[] args) throws ExecutionException, InterruptedException {
                MyCallable mc = new MyCallable();
                FutureTask<Integer> ft = new FutureTask<>(mc);
                Thread t = new Thread(ft);
                t.start();
                System.out.println(ft.get());
        }
}*/

//setname方法
//getname方法
//currentThread方法
//sleep方法
//setPriority方法
//getPriority方法

/*class MyThread extends Thread{
    public void run(){
        for (int i = 0; i < 100; i++) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(getName()+"@"+i);
        }
    }
}
public class demo2{
    public static void main(String [] args ){
        MyThread t1= new MyThread();
        t1.setName("张三");
        System.out.println(t1.getPriority());
        t1.start();
    }
}*/

/*class MyThread implements Callable<Integer>{
    public Integer call(){
        int sum=0;
        for (int i = 0; i < 100; i++) {
            sum+=i;
        }
        return sum;
    }
}

public class demo2{
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        MyThread t1 = new MyThread();
        FutureTask<Integer> ft = new FutureTask<>(t1);
        Thread t = new Thread(ft);
        t.start();
        Thread.sleep(2000);
        System.out.println(ft.get());
    }
}*/


//守护线程
/*class MyThread1 extends Thread{
    public void run(){
        for (int i = 0; i < 10; i++) {
            System.out.println(getName()+i);
        }
    }
}

class MyThread2 extends Thread{
    public void run(){
        for (int i = 0; i < 100; i++) {
            System.out.println(getName()+i);
        }
    }
}
public class demo2{
    public static void main(String[] args){
        MyThread1 t1 = new MyThread1();
        MyThread2 t2 = new MyThread2();
        t1.setName("女神");
        t2.setName("备胎");
        t2.setDaemon(true);
        t1.start();
        t2.start();
    }
}*/

/*需求：
某电影院目前正在上映国产大片，共有100张票，而它有3个窗口卖票，请设计一个程序模拟该电影院卖票
*/
/*class MyThread extends Thread{
    public static int sum = 0;
    public void run(){
            while(true){
                if(sum<100){
                    sum++;
                    System.out.println(getName()+"正在卖第"+sum+"张票");
                }else{
                    System.out.println("票买完了！");
                    break;
                }
        }
    }
}

public class demo2{
    public static void main(String[] args) {
            MyThread t1 = new MyThread();
            MyThread t2 = new MyThread();
            MyThread t3 = new MyThread();
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
            t1.start();
            t2.start();
            t3.start();
    }
}*/

//同步代码快
/*class MyThread extends Thread{
    public static int sum = 0;
    public void run(){
            while(true){
                synchronized (MyThread.class){
                    if(sum<100){
                        sum++;
                        System.out.println(getName()+"正在卖第"+sum+"张票");
                    }else{
                        System.out.println("票买完了！");
                        break;
                    }
            }
        }
    }
}

public class demo2{
    public static void main(String[] args) {
            MyThread t1 = new MyThread();
            MyThread t2 = new MyThread();
            MyThread t3 = new MyThread();
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
            t1.start();
            t2.start();
            t3.start();
    }
}*/

//同步方法
/*
*当同步方法的是非静态的时候锁对象是this
* 当同步方法是静态的时候锁对象是类名.class
* */
/*class MyThread extends Thread{
    public static int sum = 0;
    public void run(){
            while(true){
                if (mothon()) break;
            }
    }

    private synchronized boolean mothon() {
        if(sum<100){
            sum++;
            System.out.println(getName()+"正在卖第"+sum+"张票");
        }else{
            System.out.println("票买完了！");
            return true;
        }
        return false;
    }
}

public class demo2{
    public static void main(String[] args) {
            MyThread t1 = new MyThread();
            MyThread t2 = new MyThread();
            MyThread t3 = new MyThread();
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
            t1.start();
            t2.start();
            t3.start();
    }
}*/

//锁对象
/*class MyThread extends Thread{
    public static int sum = 0;
    static Lock lock = new ReentrantLock();
    public void run(){
            while(true){
                lock.lock();
                try {
                    if(sum<100){
                        sum++;
                        System.out.println(getName()+"正在卖第"+sum+"张票");
                    }else{
                        System.out.println("票买完了！");
                        break;
                    }
                }finally{
                    lock.unlock();
                }
        }
    }
}

public class demo2{
    public static void main(String[] args) {
            MyThread t1 = new MyThread();
            MyThread t2 = new MyThread();
            MyThread t3 = new MyThread();
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
            t1.start();
            t2.start();
            t3.start();
    }
}*/

//等待唤醒机智
//桌子
/*class Desk {
    public static int flagDesk = 0;
    public static int count =10;
    public static Object lock = new Object();
}
//吃货
class Foodie extends Thread{
    public void run(){
        while (true){
            synchronized (Desk.lock){
                //如果已经吃了十个蛋糕
                if(Desk.count<=0){
                    break;
                }else{//如果还没有吃完10个蛋糕
                    //如果桌子上面有蛋糕
                    if(Desk.flagDesk==1){
                        Desk.count--;
                        System.out.println("又吃了一个蛋糕,还可以吃"+Desk.count+"个");
                        Desk.flagDesk = 0;
                        Desk.lock.notifyAll();
                    }
                    //如果桌子上面没有蛋糕
                    else{
                        try {
                            Desk.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        Desk.lock.notifyAll();
                    }
                }
            }
        }
    }
}

//厨师
class Cook extends Thread{
    public void run() {
        while(true){
            synchronized (Desk.lock){
                //如果已经做了10块蛋糕
                if(Desk.count<=0){
                    break;
                }
                //如果还没有做到10块蛋糕
                else{
                    //如果桌子上面还没有蛋糕
                    if(Desk.flagDesk==0){
                        System.out.println("又做了一块蛋糕");
                        Desk.flagDesk =1;
                        Desk.lock.notifyAll();
                    }
                    //如果桌子上面有蛋糕
                    else{
                        try {
                            Desk.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
    }
}
public class demo2{
    public static void main(String[] args){
        Cook cook = new Cook();
        Foodie foodie = new Foodie();
        cook.start();
        foodie.start();
    }
}*/

//阻塞队列
/*class cook extends Thread{
    ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
    public cook(ArrayBlockingQueue<String> queue){
        this.queue = queue;
    }
    public void run(){
        while(true){
            try {
                queue.put("蛋糕");
                System.out.println("放了一个蛋糕");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

class food extends Thread{
    ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
    public food(ArrayBlockingQueue<String> queue){
        this.queue = queue;
    }
    public void run(){
        while(true){
            try {
                String food = queue.take();
                System.out.println("吃货吃了一个蛋糕");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

public class demo2{
    public static void main(String[] args) {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
        food f = new food(queue);
        cook c = new cook(queue);
        f.start();
        c.start();
    }
}*/





